<%-- 
    Document   : Customers
    Created on : Mar 16, 2014, 1:20:37 PM
    Author     : OleR
--%>

<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

<%@page import="org.simplecrm.domain.event.EventStatus" %>

<%@ include file="../urls.jsp" %>

<c:set var="lang" value="${pageContext.response.locale}"/>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="${customersViewStyleUrl}"/>
    <link rel="stylesheet" type="text/css" href="${jQueryUIStyleUrl}">

    <script type="text/javascript" src="${jQueryUrl}"></script>
    <script type="text/javascript" src="${jQueryUIUrl}"></script>
    <script type="text/javascript" src="${jsHandlersUrl}"></script>
    <script type="text/javascript" src="${jsLocalChangeUrl}"></script>
    <script>
        saveIconUrl = '${saveIconUrl}';
        editIconUrl = '${editIconUrl}';
        deleteCustomerUrl = '${deleteCustomerUrl}';
        deleteEventUrl = '${deleteEventUrl}';
        prepareUpdateEventUrl = '${prepareUpdateEventUrl}';
    </script>

    <title><spring:message code="title.custView"/></title>
</head>

<body>

<c:if test="${done eq true}">
    <script>
        $(function () {
            $("#success-done").dialog("open");
        });
    </script>
</c:if>

<%@ include file="../dialogs.jsp" %>
<%@ include file="../header.jsp" %>

<c:set var="descriptionPlaceholder">
    <spring:message code='custview.DescriptionPrompt'/>
</c:set>

<h1 style="text-align: center;"><spring:message code="custview.Caption"/></h1>

<button type="button" onclick="collapseAll();"><spring:message code="custview.CollapseAll"/></button>
<button type="button" onclick="expandAll();"><spring:message code="custview.ExpandAll"/></button>
<br/>

<br/>

<table border="0">
    <tr>
        <td class="custCaptCell" id="c1"><spring:message code="custview.CustomerName"/></td>
        <td class="custCaptCell" id="c2"><spring:message code="custview.CustomerIncome"/></td>
        <td class="custCaptCell" id="c3"><spring:message code="custview.CustomerRegion"/></td>
        <c:if test="${displayManagerContext}">
            <td class="custCaptCell" id="c4"></td>
            <td class="custCaptCell" id="c5"></td>
        </c:if>
    </tr>
</table>

<c:choose>
    <c:when test="${requestScope.custList.isEmpty()}">
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <spring:message code="custview.CustomerEmptyList"/>
    </c:when>
    <c:otherwise>


        <div class="treeview">
            <ul>
                <c:forEach var="item" items="${requestScope.custList}">
                    <li class="cl">
                        <div>
                            <p>
                                <a href="#" class="sc" onclick="return UnHide(this);">&#9658;</a>

                            <table border="0" style="padding-left: 40px">
                                <tr>
                                    <td class="custCell" id="c1">
                                        <div class="custDispl" style="display: block">${item.name}</div>
                                        <input type="text" name="custId" value="${item.id}" style="display: none;"/>
                                    </td>
                                    <td class="custCell" id="c2">
                                        <div class="custDispl" style="display: block">$${item.totalIncome}</div>
                                    </td>
                                    <td class="custCell" id="c3">
                                        <div class="custDispl" style="display: block">${item.region.name}</div>
                                    </td>
                                    <c:if test="${displayManagerContext}">
                                        <td class="custCell" id="c5">
                                            <img src="${deleteIconUrl}" class="bigIcon"
                                                 onclick="DeleteCustomer('${item.id}', '${item.name}');">
                                        </td>
                                    </c:if>
                                </tr>
                            </table>
                            </p>
                        </div>
                        <ul>
                            <div class="divSpace"></div>

                            <c:choose>
                                <c:when test="${item.events.isEmpty()}">
                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                    <spring:message code="custview.EventEmptyList"/>
                                </c:when>
                                <c:otherwise>
                                    <table border="0">
                                        <tr>
                                            <td class="eventCaptCell" id="ce1"><spring:message
                                                    code="custview.EventType"/></td>
                                            <td class="eventCaptCell" id="ce2"><spring:message
                                                    code="custview.EventDescription"/></td>
                                            <td class="eventCaptCell" id="ce3"><spring:message
                                                    code="custview.EventDate"/></td>
                                            <td class="eventCaptCell" id="ce4"><spring:message
                                                    code="custview.EventStatus"/></td>
                                            <td class="eventCaptCell" id="ce5"><spring:message
                                                    code="custview.EventBinding"/></td>
                                            <td class="eventCaptCell" id="ce6" style="display: table-cell"></td>
                                            <c:if test="${displayManagerContext}">
                                                <td class="eventCaptCell" id="ce7"></td>
                                            </c:if>
                                        </tr>
                                    </table>
                                    <c:forEach var="event" items="${item.events}" begin="0" end="4">

                                        <li>
                <span>
                    <form:form commandName="event" action="${updateEventUrl}" id="editEventForm${event.id}">
                        <c:set var="displayEdit" value="block"/>
                        <sec:authorize access="hasRole('Support')">
                            <c:if test="${event.support.login!=pageContext.request.userPrincipal.name}">
                                <c:set var="displayEdit" value="none"/>
                            </c:if>
                        </sec:authorize>
                        <table border="0">
                            <tr>
                                <td class="eventCell" id="ce1">
                                    <div class="eventDispl" style="display: block">
                                            ${event.type.name}
                                    </div>
                                    <input type="text" name="eventId" value="${event.id}" style="display: none;"/>
                                    <input type="text" name="eventOwnerId" value="${item.id}" style="display: none;"/>
                                </td>
                                <td class="eventCell" id="ce2">
                                    <div class="eventDispl" style="display: block">
                                            ${event.description}
                                    </div>
                                </td>
                                <td class="eventCell" id="ce3">
                                    <div class="eventDispl" style="display: block">
                                            ${event.creationDate}
                                    </div>
                                </td>
                                <td class="eventCell" id="ce4">
                                    <div class="eventDispl" style="display: block">
                                            ${event.status}
                                    </div>
                                </td>
                                <td class="eventCell" id="ce5">
                                    <div class="eventDispl" style="display: block">
                                            ${event.support.login==null?MSG_NONE:event.support.login}
                                    </div>
                                </td>
                                <td class="eventCell" id="ce6">
                                    <img id="eventEditIcon" src="${editIconUrl}" style="display: ${displayEdit}"
                                         class="smallIcon" onclick="EditEvent(${item.id}, ${event.id});">
                                </td>
                                <c:if test="${displayManagerContext}">
                                    <td class="eventCell" id="ce7">
                                        <!-- TODO: L10N: pass localized confirm-question to js-function -->
                                        <img src="${deleteIconUrl}" class="smallIcon"
                                             onclick="DeleteEvent('${event.id}',
                                                     '${item.name}',
                                                     '${event.description}');">
                                    </td>
                                </c:if>
                            </tr>
                        </table>
                    </form:form>
                </span>
                                    </c:forEach>
                                    </li>
                                    <br/>
                                    <spring:url var="viewEvents" value="/crm/events/view/${item.id}/1"/>
                                    <button onclick="window.location.href='${viewEvents}';">View All</button>
                                    <br/><br/>
                                </c:otherwise>
                            </c:choose>

                            <c:if test="${displayManagerContext}">
                                <form:form commandName="event" method="post" action="${newEventUrl}">
                                    <table border="0">
                                        <tr class='newItem'>
                                            <td id="ce1">
                                                <form:input path="customer" hidden="true" value="${item.id}"/>

                                                <form:select path="type" required="true">
                                                    <form:option value="" label="Select type..." disabled="true"
                                                                 selected="true"/>
                                                    <form:options items="${sessionScope.eventTypes}" itemValue="id"
                                                                  itemLabel="name"/>
                                                </form:select>
                                                <br/>
                                                <form:errors path="type" cssClass="error"/>
                                            </td>
                                            <td id="ce2">
                                                <form:input path="description" cssStyle="width:100%"
                                                            placeholder="${descriptionPlaceholder}" required="true"/>
                                                <br/>
                                                <form:errors path="description" cssClass="error"/>
                                            </td>
                                            <td id="ce3">
                                                <input id="datetimepicker" class="calendarInput" disabled="true"/>
                                            </td>
                                            <td id="ce4">
                                                <form:select path="status" required="true">
                                                    <form:option value="" label="Select status..." disabled="true"
                                                                 selected="true"/>
                                                    <form:options items="<%=EventStatus.values()%>"/>
                                                </form:select>
                                                <br/>
                                                <form:errors path="status" cssClass="error"/>
                                            </td>
                                            <td id="ce5">
                                                <form:select path="support">
                                                    <form:option value="" label="NONE" selected="true"/>
                                                    <form:options items="${sessionScope.supportList}" itemValue="id"
                                                                  itemLabel="login"/>
                                                </form:select>
                                                <br/>
                                                <form:errors path="support" cssClass="error"/>
                                            </td>
                                            <td>
                                                <input type="submit" value="<spring:message code='custview.AddEvent'/>"
                                                       style="width:100%;height:20px;float:right;">
                                            </td>
                                        </tr>
                                    </table>
                                </form:form>
                            </c:if>
                        </ul>
                    </li>
                </c:forEach>
            </ul>
        </div>

    </c:otherwise>
</c:choose>
<br/>

<c:if test="${currentPage eq 1}">
    <c:set var="prevDisabled" value="disabled"/>
</c:if>
<c:if test="${lastPage}">
    <c:set var="nextDisabled" value="disabled"/>
</c:if>
<div style="text-align: right;">
    <button onclick="window.location.href='${customersUrl}/${currentPage-1}'" ${prevDisabled}><-</button>
    <c:forEach var="no" begin="1" end="${totalPages}">
        <c:choose>
            <c:when test="${no eq currentPage}">
                ${no}
            </c:when>
            <c:otherwise>
                <a href="${customersUrl}/${no}">${no}</a>
            </c:otherwise>
        </c:choose>
    </c:forEach>
    <button onclick="window.location.href='${customersUrl}/${currentPage+1}'" ${nextDisabled}>-></button>
</div>
<br/><br/>

<c:if test="${displayManagerContext}">

    <c:set var="namePlaceholder">
        <spring:message code='custview.NamePrompt'/>
    </c:set>
    <form:form commandName="customer" method="post" action="${newCustomerUrl}">
        <table>
            <tr class='newItem'>
                <td class="custCell" id="c1" style="border: 1px solid black;">
                    <form:input path="name" placeholder="${namePlaceholder}" cssStyle="width: 90%" required="true"/>
                    <br/>
                    <form:errors path="name" cssClass="error"/>
                </td>
                <td class="custCell" id="c2" style="border: 1px solid black;">
                    <form:input path="totalIncome" type="hidden" value="0"/>
                    <input value="$0" disabled="true" style="width: 90%"/>
                </td>
                <td class="custCell" id="c3" style="border: 1px solid black;">
                    <form:select path="region" cssStyle="width: 90%" required="true">
                        <form:option value="" label="Select region..." disabled="true" selected="true"/>
                        <form:options items="${sessionScope.regions}" itemValue="id" itemLabel="name"/>
                    </form:select>
                    <br/>
                    <form:errors path="region" cssClass="error"/>
                </td>
            </tr>
            <tr>
                <td colspan="7">
                    <input type="submit" value="<spring:message code='custview.AddCustomer'/>"
                           style="width:20%;height:20px;float:right;">
                </td>
            </tr>
        </table>
    </form:form>

</c:if>

</body>
</html>
